from rest_framework.generics import ListCreateAPIView
from rest_framework.permissions import IsAdminUser

from meiduo_admin.serializers.users_ser import UserSerializer
from meiduo_admin.utils import PageNum
from users.models import User


class UserView(ListCreateAPIView):
    # 指定是用的序列器
    serializer_class = UserSerializer
    # 指定分页器
    pagination_class = PageNum
    # 指定查询集
    queryset = User.objects.filter(is_staff=False)
    # 指定权限
    permission_classes = [IsAdminUser]

    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")
        if keyword == "" or keyword is None:
            return User.objects.filter(is_staff=False)
        else:
            return User.objects.filter(is_staff=False, username__contains=keyword)
